package com.zoho.daliansheng.service;

import com.alibaba.fastjson.JSONObject;
import com.zoho.daliansheng.config.LoginConfig;
import com.zoho.daliansheng.utils.JwtUtils;
import com.zoho.daliansheng.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    LoginConfig loginConfig;

    public LoginServiceImpl(LoginConfig loginConfig) {
        this.loginConfig = loginConfig;
    }

    @Override
    public ResultUtil<Object> login(JSONObject param) {

        String userName = param.getString("userName");
        String password = param.getString("password");

        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)) {
            return ResultUtil.error("用户名或密码为空");
        }

        String system_userName = loginConfig.getUserName();
        String system_password = loginConfig.getPassword();

        //System.out.println("system_userName = " + system_userName);
        //System.out.println("system_password = " + system_password);

        if (!userName.equals(system_userName) || !password.equals(system_password)) {
            return ResultUtil.error("用户名或密码错误");
        }
        String token = JwtUtils.createToken(system_userName, system_password);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return ResultUtil.success(map);
    }
}
